package jdbc;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

/**
 * 执行查询语句
 */
public class JDBCDemo5 {
    public static void main(String[] args) {
        //将所有老师的id,name,salary,title查询出来
        try (
                Connection connection = DBUtil.getConnection();
        ){
            Statement statement = connection.createStatement();
            String sql = "SELECT id,name,salary,title " +
                         "FROM teacher";
            /*
                Statement提供的方法：
                ResultSet executeQuery(String sql)
                专门用于执行DQL语句的，返回值为查询结果集
             */
            ResultSet rs = statement.executeQuery(sql);
            /*
                boolean next()
                使结果集指针向下移动一行，如果这一行存在则返回true，否则返回false
             */
            while(rs.next()){
                //根据字段名获取该字段对应类型的值
                int id = rs.getInt("id");
                String name = rs.getString("name");
                int salary = rs.getInt("salary");
                String title = rs.getString("title");
                //也可以根据结果集每个字段的下标获取对应的值(数据库中下标从1开始)
//                int id = rs.getInt(1);
//                String name = rs.getString(2);
//                int salary = rs.getInt(3);
//                String title = rs.getString(4);
                System.out.println(id + "," + name + "," + salary + "," + title);
            }
        } catch (SQLException e) {
           e.printStackTrace();
        }
    }
}







